package bdz.projects.lavender.opsm.ui {
	import bdz.common.events.GenericEvent;
	import bdz.projects.lavender.opsm.vo.ProductVO;
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	
	/**
	 * ...
	 * @author ...
	 */
	public class ProductColorList extends Sprite {
		private var _sx:Number = 0;
		private var _dp:Array;
		private var _ref:Array;
		
		function ProductColorList() {
			addEventListener(Event.ADDED_TO_STAGE, initializeComponent);
		}
		protected function initializeComponent(evt:Event):void {
			if (_dp) {
				renderItems();
			}
		}
		protected function renderItems():void {
			_sx = 0;
			if (_ref && _ref.length) {
				for each(var sp:Sprite in _ref) {
					removeChild(sp);
				}
			}
			_dp.reverse();
			for each(var pvo:ProductVO in _dp) {
				createColorItem(pvo);
			}
		}
		protected function createColorItem(pvo:ProductVO):void {
			var sp:MovieClip = new MovieClip();
			sp.x = _sx;
			sp.buttonMode = true;
			sp["vo"] = pvo;
			sp.addEventListener(MouseEvent.CLICK, onColorClick);
			sp.graphics.beginFill(pvo.colorHex * Math.random(), 1);
			sp.graphics.drawRect(0, 0, 10, 10);
			sp.graphics.endFill();
			_sx -= sp.width + 3;
			addChild(sp);
		}
		protected function onColorClick(evt:MouseEvent):void {
			
			dispatchEvent(new GenericEvent(GenericEvent.CLICKED, { pvo:evt.target.vo } ));
		}
		public function set dataProvider(arr:Array):void {
			_dp = arr;
			if(stage){
				renderItems();
			}
		}
		public function get dataProvider():Array {
			return _dp;
		}
	}
	
}